from django.db import models
from Administrative_office_system.utils.BaseModel import BaseModel



class InfrastructureMaintenance(BaseModel):
    STATE = (
        ('draft', '草稿'),
        ('one', '基建科'),
        ('two', '需求科室'),
        ('three', '需求科室分管院长'),
        ('four', '基建科分管院长'),
        ('refuse', '拒绝'),
        ('end', '结束'),
        ('stop', '终止'),
    )
    state = models.CharField('状态', max_length=10, choices=STATE, default='one')
    apply_user = models.ForeignKey('personal_center.User', verbose_name="申请人", blank=True, null=True, on_delete=models.SET_NULL)
    company = models.ForeignKey('administrative.Company', verbose_name="上传科室", blank=False, null=False, on_delete=models.CASCADE)
    mobile = models.CharField('联系电话', max_length=50, default='')
    reason = models.CharField(max_length=255, verbose_name='申请理由', default='')
    note = models.CharField(max_length=255, verbose_name='备注', default='')
    project_name = models.CharField(max_length=255, verbose_name='项目名称', default='')
    cost_estimating = models.CharField(max_length=255, verbose_name='费用估算', default='')
    budget_details = models.CharField(max_length=255, verbose_name='预算明细', default='')
    completion_time = models.CharField(max_length=255, verbose_name='预估完成时间', default='')
    trial_date = models.CharField(max_length=255, verbose_name='审理日期', default='')
    emergency = models.CharField(max_length=10, verbose_name='应急项目', default='0')
    can_revise = models.CharField(max_length=10, verbose_name='是否可以修改', default='1')
    fj_number = models.CharField('附件编号', max_length=128, blank=True)
    current_audit_users = models.ManyToManyField('personal_center.User', verbose_name='当前审核人员', related_name='infrastructure_maintenance_users', blank=True)
    audit_users_already = models.ManyToManyField('personal_center.User', verbose_name='已审核人员', related_name='infrastructure_maintenance_already_audit_users', blank=True)

    def get_state_str(self):
        if self.state == 'refuse':
            return '拒绝'
        elif self.state == 'end':
            return '结束'
        elif self.state == 'stop':
            return '终止'
        elif self.state == 'draft':
            return '草稿'
        else:
            return '审核中'


class InfrastructureMaintenanceRecord(BaseModel):
    STATE = (
        ('draft', '拒绝'),
        ('audit', '同意'),
        ('unaudit', '待审核'),
    )
    NODE = (
        ('one', '基建科'),
        ('two', '需求科室'),
        ('three', '需求科室分管院长'),
        ('four', '基建科分管院长')
    )
    parent_id = models.ForeignKey('InfrastructureMaintenance', verbose_name="基建维修改造", blank=False, null=False, on_delete=models.CASCADE)
    state = models.CharField('状态', max_length=10, choices=STATE, default='unaudit')
    audit_people = models.ForeignKey('personal_center.User', verbose_name="审批人", blank=False, null=False, on_delete=models.CASCADE)
    audit_note = models.TextField(verbose_name="审批意见", blank=False)
    node = models.CharField('节点', max_length=10, choices=NODE, default='one')

    class Meta:
        managed = True
        verbose_name = '基建维修改造'
        verbose_name_plural = verbose_name


class ProfessionalServices(BaseModel):
    STATE = (
        ('draft', '草稿'),
        ('one', '科室负责人'),
        ('two', '基建科负责人'),
        ('three', '基建科分管院长'),
        ('refuse', '拒绝'),
        ('end', '结束'),
        ('stop', '终止'),
    )
    state = models.CharField('状态', max_length=10, choices=STATE, default='one')
    apply_user = models.ForeignKey('personal_center.User', verbose_name="申请人", blank=True, null=True, on_delete=models.SET_NULL)
    company = models.ForeignKey('administrative.Company', verbose_name="上传科室", blank=False, null=False, on_delete=models.CASCADE)
    mobile = models.CharField('联系电话', max_length=50, default='')
    reason = models.CharField(max_length=255, verbose_name='申请理由', default='')
    note = models.CharField(max_length=255, verbose_name='备注', default='')
    service_content = models.CharField(max_length=255, verbose_name='服务内容', default='')
    budget_amount = models.CharField(max_length=255, verbose_name='预算金额', default='')
    amount_words = models.CharField(max_length=255, verbose_name='金额大写', default='')
    can_revise = models.CharField(max_length=10, verbose_name='是否可以修改', default='1')
    fj_number = models.CharField('附件编号', max_length=128, blank=True)
    current_audit_users = models.ManyToManyField('personal_center.User', verbose_name='当前审核人员', related_name='professional_services_audit_users', blank=True)
    audit_users_already = models.ManyToManyField('personal_center.User', verbose_name='已审核人员', related_name='professional_services_already_audit_users', blank=True)

    def get_state_str(self):
        if self.state == 'refuse':
            return '拒绝'
        elif self.state == 'end':
            return '结束'
        elif self.state == 'stop':
            return '终止'
        elif self.state == 'draft':
            return '草稿'
        else:
            return '审核中'


class ProfessionalServicesRecord(BaseModel):
    STATE = (
        ('draft', '拒绝'),
        ('audit', '同意'),
        ('unaudit', '待审核'),
    )
    NODE = (
        ('one', '科室负责人'),
        ('two', '基建科负责人'),
        ('three', '基建科分管院长')
    )
    parent_id = models.ForeignKey('ProfessionalServices', verbose_name="基建项目专业服务", blank=False, null=False, on_delete=models.CASCADE)
    state = models.CharField('状态', max_length=10, choices=STATE, default='unaudit')
    audit_people = models.ForeignKey('personal_center.User', verbose_name="审批人", blank=False, null=False, on_delete=models.CASCADE)
    audit_note = models.TextField(verbose_name="审批意见", blank=False)
    node = models.CharField('节点', max_length=10, choices=NODE, default='one')

    class Meta:
        managed = True
        verbose_name = '基建项目专业服务'
        verbose_name_plural = verbose_name